/*
 * @lc app=leetcode.cn id=39 lang=cpp
 * @lcpr version=30204
 *
 * [39] 组合总和
 */

// @lcpr-template-start
using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// @lcpr-template-end
// @lc code=start
class Solution
{
public:
    vector<vector<int>> res;
    vector<int> cur;
    vector<vector<int>> combinationSum(vector<int> &candidates, int target)
    {
        DFS(candidates, target, 0, 0);
        return res;
    }

private:
    void DFS(vector<int> &candidates, int target, int index, int curSum)
    {
        if (curSum > target)
            return;
        else if (curSum == target)
        {
            res.push_back(cur);
            return;
        }

        for (int i = index; i < candidates.size(); ++i)
        {
            cur.push_back(candidates[i]);
            DFS(candidates, target, i, curSum + candidates[i]);
            cur.pop_back();
        }
    }
};
// @lc code=end

/*
// @lcpr case=start
// [2,3,6,7]\n7\n
// @lcpr case=end

// @lcpr case=start
// [2,3,5]\n8\n
// @lcpr case=end

// @lcpr case=start
// [2]\n1\n
// @lcpr case=end

 */
